/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.TX.tta;

import Helper.TTAHelper.Helper_MDSCluster;
import Helper.TTAHelper.Helper_ReservationByAge;
import Helper.TTAHelper.Helper_ReservationByCountry;
import Helper.TTAHelper.Helper_SalesByAge;
import Helper.TTAHelper.Helper_SalesByCountry;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import managedbean.TX.CustomerIOTRPortal.FakeMainPortalMB;
import org.primefaces.component.chart.series.ChartSeries;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.PieChartModel;
import session.TX.TTADisplaySBRemote;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "mdsStakeholderMB")
@ViewScoped
public class MDSStakeholderMB {

    @ManagedProperty(value = "#{fakeMainPortalMB}")
    private FakeMainPortalMB portalManagedBean;
    @EJB
    private TTADisplaySBRemote ttaBean;
    private PieChartModel salesByAgeModel;
    private PieChartModel salesByCountryModel;
    private PieChartModel reservationByCountryModel;
    private PieChartModel reservationByAgeModel;
    private PieChartModel top10ClusterModel;
    private CartesianChartModel salesByAgeCartesian;
    private CartesianChartModel salesByCountryCartesian;
    private CartesianChartModel reservationByAgeCartesian;
    private CartesianChartModel reservationByCountryCartesian;
    private CartesianChartModel topClusterCartesian;
    private int salesByAgeChart = 1;
    private int salesByCountryChart = 2;
    private int reservationByAgeChart = 1;
    private int reservationByCountryChart = 2;
    private int topClusterChart = 1;

    /** Creates a new instance of MDSOutletMB */
    public MDSStakeholderMB() {
    }

    public int getSalesByAgeChart() {
        return salesByAgeChart;
    }

    public void setSalesByAgeChart(int salesByAgeChart) {
        this.salesByAgeChart = salesByAgeChart;
    }

    public int getReservationByAgeChart() {
        return reservationByAgeChart;
    }

    public void setReservationByAgeChart(int reservationByAgeChart) {
        this.reservationByAgeChart = reservationByAgeChart;
    }

    public int getReservationByCountryChart() {
        return reservationByCountryChart;
    }

    public void setReservationByCountryChart(int reservationByCountryChart) {
        this.reservationByCountryChart = reservationByCountryChart;
    }

    public int getSalesByCountryChart() {
        return salesByCountryChart;
    }

    public void setSalesByCountryChart(int salesByCountryChart) {
        this.salesByCountryChart = salesByCountryChart;
    }

    public int getTopClusterChart() {
        return topClusterChart;
    }

    public void setTopClusterChart(int topClusterChart) {
        this.topClusterChart = topClusterChart;
    }

    public void chartTypeUpdate() {
    }

    private List<Helper_SalesByAge> getSalesByAge() {
        if (portalManagedBean.getStakeholderName() == null) {
            return null;
        }
        List<Helper_SalesByAge> returnList = ttaBean.getSSalesByAge(portalManagedBean.getStakeholderName());
        return returnList;
    }

    private List<Helper_SalesByCountry> getSalesByCountry() {
        if (portalManagedBean.getStakeholderName() == null) {
            return null;
        }
        List<Helper_SalesByCountry> returnList = ttaBean.getSSalesByCountry(portalManagedBean.getStakeholderName());
        return returnList;
    }

    private List<Helper_ReservationByAge> getReservationByAge() {
        if (portalManagedBean.getStakeholderName() == null) {
            return null;
        }
        List<Helper_ReservationByAge> returnList =
                ttaBean.getSReservationByAge(portalManagedBean.getStakeholderName());
        return returnList;
    }

    private List<Helper_ReservationByCountry> getReservationByCountry() {
        if (portalManagedBean.getStakeholderName() == null) {
            return null;
        }
        List<Helper_ReservationByCountry> returnList =
                ttaBean.getSReservationByCountry(portalManagedBean.getStakeholderName());
        return returnList;
    }

    private List<Helper_MDSCluster> getAllClusters() {
        if (portalManagedBean.getStakeholderName() == null) {
            return null;
        }
        List<Helper_MDSCluster> returnList =
                ttaBean.getSAllClusters(portalManagedBean.getStakeholderName());
        return returnList;
    }

    private void createSalesByAgeModel() {
        if (salesByAgeModel == null) {
            salesByAgeModel = new PieChartModel();
            List<Helper_SalesByAge> allRecords = this.getSalesByAge();
            for (Helper_SalesByAge aRecord : allRecords) {
                System.out.println("Record:" + aRecord.getGroup() + "," + aRecord.getSales());
                salesByAgeModel.set(aRecord.getGroup(), aRecord.getSales());
            }
        }
    }

    private void createSalesByCountryModel() {
        if (salesByCountryModel == null) {
            salesByCountryModel = new PieChartModel();
            List<Helper_SalesByCountry> allRecords = this.getSalesByCountry();
            Double totalSale = new Double(0);
            for (Helper_SalesByCountry aRecord : allRecords) {
                totalSale += aRecord.getSales();
            }
            Double otherSale = totalSale;
            for (Helper_SalesByCountry aRecord : allRecords) {
                //only display countries which acounts for more than 2% of total sales
                if (totalSale / aRecord.getSales() <= 50) {
                    salesByCountryModel.set(aRecord.getGroup(), aRecord.getSales());
                    otherSale -= aRecord.getSales();
                }
            }
            salesByCountryModel.set("Others", otherSale);
        }
    }

    private void createReservationByAgeModel() {
        if (reservationByAgeModel == null) {
            reservationByAgeModel = new PieChartModel();

            List<Helper_ReservationByAge> allRecords = this.getReservationByAge();
            for (Helper_ReservationByAge aRecord : allRecords) {
                reservationByAgeModel.set(aRecord.getGroup(), aRecord.getSales());
            }
        }
    }

    private void createReservationByCountryModel() {
        if (reservationByCountryModel == null) {
            reservationByCountryModel = new PieChartModel();
            List<Helper_ReservationByCountry> allRecords = this.getReservationByCountry();
            Double totalSale = new Double(0);
            for (Helper_ReservationByCountry aRecord : allRecords) {
                totalSale += aRecord.getSales();
            }
            Double otherSale = totalSale;
            for (Helper_ReservationByCountry aRecord : allRecords) {
                //only display countries which acounts for more than 2% of total sales
                if (totalSale / aRecord.getSales() <= 50) {
                    reservationByCountryModel.set(aRecord.getGroup(), aRecord.getSales());
                    otherSale -= aRecord.getSales();
                }
            }
            reservationByCountryModel.set("Others", otherSale);
        }
    }

    private void createTop10ClusterModel() {
        if (top10ClusterModel == null) {
            top10ClusterModel = new PieChartModel();

            List<Helper_MDSCluster> allRecords = this.getAllClusters();
            for (int i = 0; i < 10; i++) {
                top10ClusterModel.set(allRecords.get(i).getDescription(), allRecords.get(i).getNumber());
            }
            Long rest = new Long(0);
            for (int i = 10; i < allRecords.size(); i++) {
                rest += allRecords.get(i).getNumber();
            }
            top10ClusterModel.set("Others", rest);
        }
    }

    private void createSalesByAgeCartesian() {
        if (salesByAgeCartesian == null) {
            salesByAgeCartesian = new CartesianChartModel();
            ChartSeries sales = new ChartSeries();
            sales.setLabel("sales");
            List<Helper_SalesByAge> allRecords = this.getSalesByAge();
            for (Helper_SalesByAge aRecord : allRecords) {
                sales.set(aRecord.getGroup(), aRecord.getSales());
            }
            salesByAgeCartesian.addSeries(sales);
        }
    }

    private void createSalesByCountryCartesian() {
        if (salesByCountryCartesian == null) {
            salesByCountryCartesian = new CartesianChartModel();
            ChartSeries sales = new ChartSeries();
            sales.setLabel("sales");
            List<Helper_SalesByCountry> allRecords = this.getSalesByCountry();
            for (Helper_SalesByCountry aRecord : allRecords) {
                if (aRecord.getSales().longValue() == 0) {
                    break;
                }
                sales.set(aRecord.getGroup(), aRecord.getSales());
            }
            salesByCountryCartesian.addSeries(sales);
        }
    }

    private void createReservationByAgeCartesian() {
        if (reservationByAgeCartesian == null) {
            reservationByAgeCartesian = new CartesianChartModel();
            ChartSeries sales = new ChartSeries();
            sales.setLabel("reservations");
            List<Helper_ReservationByAge> allRecords = this.getReservationByAge();
            for (Helper_ReservationByAge aRecord : allRecords) {
                sales.set(aRecord.getGroup(), aRecord.getSales());
            }
            reservationByAgeCartesian.addSeries(sales);
        }
    }

    private void createReservationByCountryCartesian() {
        if (reservationByCountryCartesian == null) {
            reservationByCountryCartesian = new CartesianChartModel();
            ChartSeries sales = new ChartSeries();
            sales.setLabel("reservations");
            List<Helper_ReservationByCountry> allRecords = this.getReservationByCountry();
            for (Helper_ReservationByCountry aRecord : allRecords) {
                if (aRecord.getSales().longValue() == 0) {
                    break;
                }
                sales.set(aRecord.getGroup(), aRecord.getSales());
            }
            reservationByCountryCartesian.addSeries(sales);
        }
    }

    private void createTopClusterCartesian() {
        if (topClusterCartesian == null) {
            topClusterCartesian = new CartesianChartModel();
            ChartSeries clusters = new ChartSeries();
            clusters.setLabel("cluster");
            List<Helper_MDSCluster> allRecords = this.getAllClusters();
            for (int i = 0; i < 10; i++) {
                clusters.set(allRecords.get(i).getDescription(), allRecords.get(i).getNumber());
            }
            Long rest = new Long(0);
            for (int i = 10; i < allRecords.size(); i++) {
                rest += allRecords.get(i).getNumber();
            }
            clusters.set("others", rest);
            topClusterCartesian.addSeries(clusters);
        }
    }

    public FakeMainPortalMB getPortalManagedBean() {
        return portalManagedBean;
    }

    public void setPortalManagedBean(FakeMainPortalMB portalManagedBean) {
        this.portalManagedBean = portalManagedBean;
    }

    public PieChartModel getSalesByAgeModel() {
        createSalesByAgeModel();
        return salesByAgeModel;
    }

    public CartesianChartModel getSalesByAgeCartesian() {
        createSalesByAgeCartesian();
        return salesByAgeCartesian;
    }

    public PieChartModel getSalesByCountryModel() {
        createSalesByCountryModel();
        return salesByCountryModel;
    }

    public CartesianChartModel getSalesByCountryCartesian() {
        createSalesByCountryCartesian();
        return salesByCountryCartesian;
    }

    public PieChartModel getReservationByAgeModel() {
        createReservationByAgeModel();
        return reservationByAgeModel;
    }

    public CartesianChartModel getReservationByAgeCartesian() {
        createReservationByAgeCartesian();
        return reservationByAgeCartesian;
    }

    public PieChartModel getReservationByCountryModel() {
        createReservationByCountryModel();
        return reservationByCountryModel;
    }

    public CartesianChartModel getReservationByCountryCartesian() {
        createReservationByCountryCartesian();
        return reservationByCountryCartesian;
    }

    public PieChartModel getTop10ClusterModel() {
        createTop10ClusterModel();
        return top10ClusterModel;
    }

    public CartesianChartModel getTopClusterCartesian() {
        createTopClusterCartesian();
        return topClusterCartesian;
    }
    
}
